package sleuthkit.gui.actionlistener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import org.apache.log4j.Logger;

import sleuthkit.gui.MainApplication;
import sleuthkit.gui.filters.AddEvidenceFileFilter;
import sleuthkit.gui.filters.MyFileFilter;
import sleuthkit.util.CaseManagement;

public class AddEvidenceActionListener implements ActionListener {

	private MainApplication parent;
	Logger log = Logger.getLogger(AddEvidenceActionListener.class);
	AddEvidenceFileFilter addEvidenceFileFilter = new AddEvidenceFileFilter();
	
	public AddEvidenceActionListener(MainApplication parent) {
		// TODO Auto-generated constructor stub
		this.parent= parent;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JFileChooser fc = new JFileChooser();
		fc.setFileFilter(addEvidenceFileFilter);
		fc.setMultiSelectionEnabled(true);
		fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		CaseManagement cm = CaseManagement.getInstance();
		
		int returnVal = fc.showOpenDialog(parent.getFrame());
		
		MyFileFilter choosenFileFilter = new MyFileFilter(fc.getFileFilter());
		
		if (returnVal == JFileChooser.APPROVE_OPTION) {
            File[] files = fc.getSelectedFiles();
            //This is where a real application would open the file.
            for (File file : files) {
            	if (choosenFileFilter.accept(file)){
            		if (file.isDirectory()){
            			log.debug("File is a directory.");
            			log.debug("Add content to the case " + cm.getCase());
            			cm.addEvidenceFiles(listFiles(file,choosenFileFilter));
            		} else {

            			cm.addEvidenceFile(file);
            			log.debug("Opening: " + file.getName() + ".");

            		}
            		MainApplication.getInstance().addEvidenceFileToAddEvidenceJPanel(file,choosenFileFilter);
            	}
            }
        } else {
            log.debug("Open command cancelled by user.");
        }
		
	}

	private  ArrayList<File> listFiles(File file, FileFilter choosenFileFilter) {
		// TODO Auto-generated method stub
		ArrayList<File> files = new ArrayList<File>();
		log.debug("created a ArrayList<File> object");
		log.debug("useing file filter " + choosenFileFilter.getDescription());
		if (choosenFileFilter.accept(file)){
			log.debug("accepted file " + file.getAbsolutePath());
			if (file.isDirectory()){
				log.debug(file.getAbsolutePath() + " is a directory");
				File[] filesInFolder = file.listFiles();
				log.debug(file.getAbsolutePath() + " containes " + filesInFolder.length + " files");
				for (File file2 : filesInFolder) {
					log.debug("Currently processing file " + file2.getAbsolutePath());
					files.addAll(listFiles(file2,choosenFileFilter));
				}
			} else {
				log.debug(file.getAbsolutePath() + " is not a directory");
				files.add(file);

			}
		} else {
			log.debug("rejected the file " + file.getAbsolutePath());
		}
		return files;
	}

}
